/**
 * 多分报关单一起提交
 */
$.fn.multiDeclarationPayment = function () {
    var $jqgrid = this; //this 是jqgrid对象
    var __methods = {
        getIds: function () {
            return $jqgrid.jqGrid('getGridParam', 'selarrrow');
        },
        getId: function () {
            return $jqgrid.jqGrid("getGridParam", "selrow")
        },
        getRow: function (id) {
            return $jqgrid.jqGrid("getRowData", id);
        },
        getSelectRow: function () {
            return this.getRow(this.getId());
        },
        getSelectRows: function () {
            var idArr = this.getIds();
            var rowArr = [];
            if (idArr.length > 0) {
                for (var i = 0; i < idArr.length; i++) {
                    var id = idArr[i];
                    rowArr.push(this.getRow(id));
                }
            }
            return rowArr;
        },
        collectCellValues: function (rowArr, cellName) {
            var cellVals = [];
            for (var i = 0; i < rowArr.length; i++) {
                var row = rowArr[i];
                var cellValue = row[cellName];
                if (cellValue != undefined) {
                    cellVals.push(row[cellName]);
                }
            }
            return cellVals;
        },
        collectBusinessIds: function (businessLabels) {
            var bids = [];
            for (var i = 0; i < businessLabels.length; i++) {
                bids.push(businessLabels[i].match(/>\w*</)[0].replace(/[><]/g, ''));
            }
            return bids;
        },
        didMultiPay: function () {
            var rows = this.getSelectRows();
            if (rows.length <= 5) {
                var drafs = [];
                var submiteds = [];
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    var state = row.state;
                    if (state == 'DS00') {//收集草稿订单
                        drafs.push(row);
                    }
                    if (state == 'DS01') {//收集已提交订单
                        submiteds.push(row);
                    }
                }
                if (drafs.length > 0) {//存在草稿订单，提示用户，终止支付
                    var draftBids = this.collectBusinessIds(
                        this.collectCellValues(drafs, "businessId"));
                    var msg = "发现以下单证不符合支付条件的报关单：\n";
                    for (i = 0; i < draftBids.length; i++) {
                        var drftbid = draftBids[i];
                        msg += drftbid + "\n";
                    }
                    msg += "\n请您提交这些报关单后再来支付。";
                    alert(msg);
                    return;
                }
                if (submiteds.length > 0) {//提示用户确认支付
                    var submitedBids = this.collectBusinessIds(
                        this.collectCellValues(submiteds, "businessId"));
                    msg = "请您确认以下业务编号：\n";
                    for (i = 0; i < submitedBids.length; i++) {
                        var bid = submitedBids[i];
                        msg += bid + "\n";
                    }
                    msg += "\n点击确认前往支付。";
                    if (confirm(msg)) {
                        this.gotoConfirmPay(this.getIds());
                    }
                }
            } else {
                alert('一次最多选择只能支付5票单');
            }
        },
        gotoConfirmPay: function (declarationIds) {
            var param = declarationIds.join("&declaration_id=");
            location.href = '/business/declaration-confirmPay?declaration_id=' + param;
        }
    };
    __methods.didMultiPay();
};
